<?php
	class Answer extends MY_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->model('question_model');
			$this->load->model('answer_model');
			$this->load->model('trivia_model');
		}
		
		public function index($limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->answer_model->pager($limit);
			$my_link = "";
			
			// define the link for pagination
			$my_link = site_url('answer');				
		
			$data['answer'] = $this->answer_model->get_answer();
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->answer_model->get_total(), $this->answer_model->get_record_per_page());
			
			$data['question'] = $this->get_list_game_question();
			
			$data['title'] = "Answer list";
			$this->load->view('templates/header1', $data);
			$this->load->view('answer/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function filter($q_id=FALSE, $limit=FALSE){
			$this->load->helper("form");
			//set the limit or offset for each page
			$this->answer_model->pager($limit);
				
			// define the link for pagination
			$my_link = site_url('answer/filter/'.$q_id);				
		
			$data['answer'] = $this->answer_model->get_answer(FALSE, $q_id);
			
			// call the pagination from parent controller			
			$this->pager($my_link, $this->answer_model->get_total_filter($q_id), $this->answer_model->get_record_per_page(), 4);
			
			$data['question'] = $this->get_list_game_question();
			
			$data['q_id'] = $q_id;
			$data['title'] = "Answer list";
			$this->load->view('templates/header1', $data);
			$this->load->view('answer/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function search($src=FALSE,$limit=FALSE){
			$this->load->helper("form");
			$data['title'] = "Answer list";
			$search_text = '';
			
			//set the limit or offset for each page
			$this->answer_model->pager($limit);			
			
			if($src!=FALSE){				
				$data['answer'] = $this->answer_model->search($src);		
				$search_text = $src;
			}
			else{
				$search_text = $this->input->post('search_text');				
				$data['answer'] = $this->answer_model->search($search_text);		
			}
			
			// define the link for pagination
			$my_link = site_url('answer/search/'.$search_text);
			
			// call the pagination from parent controller get_total_search
			$this->pager($my_link, $this->answer_model->get_total_search($search_text), $this->answer_model->get_record_per_page(), 4);
			
			$data['question'] = $this->get_list_game_question();
			
			$this->load->view('templates/header1', $data);
			$this->load->view('answer/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}
		
		public function displayForm($id=FALSE){			
				$this->load->helper("form");
				$data = array();
				if($id!=FALSE){
					$data['answer'] = $this->answer_model->get_answer($id, FALSE);				
				}				
				$data['title']="Create Answer";				
				
				$data['question'] = $this->get_list_game_question();				
				
				$this->load->view('templates/header1', $data);
				$this->load->view('answer/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			
		}
		
		public function create(){
			$this->displayForm();
		}
		
		public function edit($id){
			$this->displayForm($id);
		}
		
		public function delete($id){
			$this->answer_model->delete($id);
			redirect("answer", "refresh");
		}
		
		public function get_list_game_question(){
			$arr = $this->trivia_model->get_trivia_list();
			if(count($arr)){
				foreach($arr as &$v){
					$v['quest'] = $this->question_model->get_question_list($v['id']);
				}
			}return $arr;
		}
		
		public function save(){			
			
			$this->load->helper("form");
			$this->load->library('form_validation');
			$this->form_validation->set_rules('answer', 'Answer', 'required');
			if ($this->form_validation->run() === FALSE){
				$data['question'] = $this->question_model->get_question_list();
				$id = $this->input->post('id');
				if(!empty($id))
					$data['answer'] = $this->question_model->get_question($id);	
				$data['title']="Create Answer";
				$this->load->view('templates/header1', $data);
				$this->load->view('answer/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$this->answer_model->save();
				redirect("answer", "refresh");
			}			
		}	
		
	}
?>